<template>
    <BaseDialog
      :title="(!props.item.electricId ? '新增' : '编辑') + '涉电名词'"
      :handleCancel="handleCancel"
      :dialogVisible="props.dialogVisible"
    >
      <BaseForm
        ref="baseForm"
        type="rule"
        :handleOk="handleOk"
        :handleCancel="handleCancel"
        :items="formItems"
        v-model:form="ruleForm"
        :rules="rules"
        :refresh="handleSearch"
        label-width="110px"
      >
        <template #select-pop>
          <SelectedTags
            keyName="communityName"
            keyId="communityId"
            v-model:list="communityList"
          />
        </template>
      </BaseForm>
    </BaseDialog>
  </template>
  
  <script setup lang="ts">
  import BaseForm from '@/components/base-form.vue'
  import BaseDialog from '@/components/base-dialog.vue'
  import { basicConfigService } from '@/api/basicConfig'
  const props = defineProps({
    dialogVisible: {
      type: Boolean,
    },
    item: {
      type: Object,
    },
    handleSearch: {
      type: Function,
    },
  })
  const emit = defineEmits(['update:dialogVisible'])
  const baseForm = ref()
  const formItems = ref([
    {
      name: '涉电名词',
      key: 'electricWord',
      type: 'input',
      maxlength: '20',
    },
  ])
  const communityList = ref([])
  const rules = reactive({
    electricWord: [{ required: true, message: '请输入', trigger: 'change' }],
  })
  const ruleForm = ref({
    electricId: '',
    electricWord: '',
  })
  
  const handleOk = () => {
    if (props.item.electricId) {
      return basicConfigService.electricUpdate({
        data: {
          ...ruleForm.value,
        },
      })
    } else {
      return basicConfigService.electricSave({
        data: {
          ...ruleForm.value,
        },
      })
    }
  }
  
  const handleCancel = () => {
    emit('update:dialogVisible', false)
  }
  onMounted(() => {
    if (props.item.electricId) {
      ruleForm.value.electricId = props.item.electricId
      ruleForm.value.electricWord = props.item.electricWord
    }
  })
  </script>
  
  <style lang="scss" scoped></style>
  